<template>
  <el-card shadow="never" class="mb10">
    <el-row>
      <el-form ref="formQueryRef" :inline="true" :model="queryData">
        <el-form-item label="姓名" prop="nickname">
          <el-input
            placeholder="请输入姓名"
            v-model.trim="queryData.nickname"
            clearable
          />
        </el-form-item>
        <el-form-item label="手机号码" prop="mobileno">
          <el-input
            placeholder="请输入手机号码"
            v-model.trim="queryData.mobileno"
            clearable
          />
        </el-form-item>
        <el-form-item label="标签条编码" prop="labelCode">
          <el-input
            placeholder="请输入标签条编码"
            v-model="queryData.labelCode"
            clearable
          />
        </el-form-item>
        <el-form-item label="核销时间" prop="cancelDates">
          <el-date-picker
            v-model="queryData.cancelDates"
            type="datetimerange"
            clearable
            value-format="YYYY-MM-DD HH:mm:ss"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          />
        </el-form-item>
      </el-form>
      <div class="flex-right">
        <el-button type="primary" :icon="RefreshLeft" @click="resetForm">
          重置
        </el-button>
        <el-button
          type="primary"
          :icon="Search"
          class="ml20"
          @click="initData"
          v-enter="initData"
        >
          搜索
        </el-button>
      </div>
    </el-row>
  </el-card>
  <el-card shadow="never">
    <div class="mb10">
      <el-button type="primary" @click="dialogImportRef.openDialog()">
        导出
      </el-button>
    </div>

    <el-table border stripe :data="tableData" v-loading="loading">
      <el-table-column
        v-for="item in options"
        :key="item.type"
        :prop="item.props"
        :type="item.type"
        :label="item.label"
        :width="item.width"
        :align="item.align"
        show-overflow-tooltip
        :fixed="item.fixed"
      >
        <template v-slot:default="{ row }" v-if="item.props === 'cancelDate'">
          {{
            row.cancelDate
              ? dayjs(row.cancelDate).format("YYYY-MM-DD HH:mm:ss")
              : ""
          }}
        </template>
        <template v-slot:default="{ row }" v-if="item.props === 'actions'">
          <el-button
            type="primary"
            text
            size="small"
            @click="onDialogStaff(row)"
          >
            明细
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      class="mt-4"
      :total="total"
      :page-size="queryData.pageSize"
      :page-sizes="[10, 20, 50, 100]"
      layout="total, sizes, prev, pager, next"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
  </el-card>

  <DialogComp ref="dialogCompRef" :batchno="batchno"></DialogComp>
  <DialogImport ref="dialogImportRef" :queryData="queryData"></DialogImport>
</template>

<script setup>
import { onMounted, ref } from "vue";
import { Search, RefreshLeft } from "@element-plus/icons-vue";
import { dayjs } from "element-plus";
import api from "@/api/report.js";
import { options } from "./options.js";
import DialogComp from "./components/dialogComp.vue";
import DialogImport from "./components/dialogImport.vue";

const queryData = ref({
  pageNo: 1,
  pageSize: 10,
});

const dialogCompRef = ref();
const dialogImportRef = ref();

const formQueryRef = ref();
const batchno = ref();
const loading = ref(false);
const tableData = ref([]);
const total = ref(0);

const initData = async () => {
  loading.value = true;
  try {
    const res = await api.cancelLabelPage({
      ...queryData.value,
      cancelDates: queryData.value.cancelDates?.length
        ? JSON.stringify(queryData.value.cancelDates)
        : undefined,
    });
    tableData.value = res.data.list;
    total.value = res.data.total;
  } finally {
    loading.value = false;
  }
};
const resetForm = () => {
  formQueryRef.value.resetFields();
};
const handleCurrentChange = (val) => {
  queryData.value.pageNo = val;
  initData(queryData.value);
};
const handleSizeChange = (val) => {
  queryData.value.pageSize = val;
  initData(queryData.value);
};

const onDialogStaff = (row) => {
  // batchno.value = row.batchno;
  // console.log(row.batchno);
  dialogCompRef.value.openDialog(row);
};

onMounted(() => {
  initData();
});
</script>
